package com.jltx.xq.simplewatch.video.adapter;

import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.news.model.bean.TopicNews;
import com.jltx.xq.simplewatch.utils.GlideUtils;
import com.jltx.xq.simplewatch.utils.TimeUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import fm.jiecao.jcvideoplayer_lib.OnVideoClickListener;

/**
 * @author jltxseo
 * @date 2017/09/07.
 * @description:
 */

public class TopicNewsAdapter extends BaseQuickAdapter<TopicNews,BaseViewHolder>{

    private static final int DEFAULT_NEWS = 0;
    /**
     * 纯文字布局(文章、广告)
     */
    private static final int TEXT_NEWS = 100;
    /**
     * 居中大图布局(1.单图文章；2.单图广告；3.视频，中间显示播放图标，右侧显示时长)
     */
    private static final int CENTER_SINGLE_PIC_NEWS = 200;
    /**
     * 右侧小图布局(1.小图新闻；2.视频类型，右下角显示视频时长)
     */
    private static final int RIGHT_PIC_VIDEO_NEWS = 300;
    /**
     * 三张图片布局(文章、广告)
     */
    private static final int THREE_PICS_NEWS = 400;

    /**
     * 视频列表类型
     */
    private static final int VIDEO_LIST_NEWS = 500;

    @IntDef({DEFAULT_NEWS,TEXT_NEWS, CENTER_SINGLE_PIC_NEWS, RIGHT_PIC_VIDEO_NEWS, THREE_PICS_NEWS, VIDEO_LIST_NEWS})

    @Retention(RetentionPolicy.SOURCE)
    public @interface ViewType {}

    private boolean isTop; //是否有置顶专题

    private boolean isVideoList; //是否是视频列表

    private OnVideoClickToStartListener onVideoClickToStartListener;

    public TopicNewsAdapter(@Nullable List<TopicNews> data, boolean isTop, boolean isVideoList) {
        super(data);
        this.isTop = isTop;
        this.isVideoList = isVideoList;
        //对于支持多子View进行支持
        multiTypeDelegate.registerItemType(VIDEO_LIST_NEWS, R.layout.item_news_video); //注册多子View布局
        setMultiTypeDelegate(multiTypeDelegate);

    }

    public void setOnVideoClickToStartListener(OnVideoClickToStartListener onVideoClickToStartListener) {
        this.onVideoClickToStartListener = onVideoClickToStartListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, TopicNews item) {
        if (TextUtils.isEmpty(item.getTitle())){
            //如果没有标题，则直接跳过
            return;
        }

        //根据类型不同的ItemType条目设置数据
        switch (helper.getItemViewType()){
            case VIDEO_LIST_NEWS:
                helper.setVisible(R.id.ll_title,true);//显示标题栏
                helper.setText(R.id.tv_title, item.getTitle());//设置标题

                //处理播放次数的字符串
                String format = mContext.getString(R.string.video_play_count);
                int watchCount = item.getVideo_detail_info().video_watch_count;
                String countUnit = "";
                if (watchCount> 10000){
                    watchCount = watchCount / 10000;
                    countUnit = "万";
                }

                helper.setText(R.id.tv_watch_count, String.format(format, watchCount + countUnit));//播放次数

                GlideUtils.loadRound(mContext, item.getUser_info().avatar_url, helper.getView(R.id.iv_avatar));//作者头像
                helper.addOnClickListener(R.id.iv_avatar); //添加头像点击事件

                helper.setVisible(R.id.ll_duration, true);//显示时长
                helper.setText(R.id.tv_duration, TimeUtils.secToTime(item.getVideo_duration()));//设置时长

                helper.setText(R.id.tv_author, item.getUser_info().name);//昵称
                helper.addOnClickListener(R.id.tv_focus); //添加关注按钮点击事件

                helper.addOnClickListener(R.id.img_more); //添加三点的更多按钮的点击事件

                helper.setText(R.id.tv_comment_count, String.valueOf(item.getComment_count()));//评论数
                JCVideoPlayerStandard videoPlayer = helper.getView(R.id.video_player);
                GlideUtils.load(mContext, item.getVideo_detail_info().detail_video_large_image.url, videoPlayer.thumbImageView, R.color.video_place_holder);//设置视频初始缩略图
                videoPlayer.setAllControlsVisible(View.GONE, View.GONE, View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, View.GONE);
                videoPlayer.tinyBackImageView.setVisibility(View.GONE);
                videoPlayer.setPosition(helper.getAdapterPosition()); //关联Position

                videoPlayer.setOnVideoClickListener(new OnVideoClickListener() {
                    @Override
                    public void onVideoClickToStart() {
                        if(onVideoClickToStartListener != null){
                            onVideoClickToStartListener.onVideoClickToStar(helper,item,videoPlayer);
                        }
                    }
                });
                break;
        }
    }

    private MultiTypeDelegate<TopicNews> multiTypeDelegate = new MultiTypeDelegate<TopicNews>() {
        @Override
        protected @ViewType int getItemType(TopicNews topicNews) {
            if(isVideoList){
                return VIDEO_LIST_NEWS;
            }
            return DEFAULT_NEWS;
        }
    };

    public interface OnVideoClickToStartListener{
        void onVideoClickToStar(BaseViewHolder helper,TopicNews topicNews,JCVideoPlayerStandard videoPlayer);
    }

}
